package com.sub.spark.sql.source;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

import java.util.Properties;

/**
 * @ClassName MysqlSource
 * @Description: 从Mysql读取数据
 * @Author Submerge.
 * @Since 2025/5/24 19:50
 * @Version 1.0
 */
public class MysqlSource {

    public static void main(String[] args) {

        SparkSession sparkSession = SparkSession.builder().appName("sub-spark-mysql").master("local[2]").getOrCreate();

        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "1234");


        Dataset<Row> jdbc = sparkSession.read().jdbc("jdbc:mysql://localhost:3306/test", "mc_user", properties);

        //创建临时表，注意，临时表是会话级别的，会话结束，临时表就消失了
        jdbc.createOrReplaceTempView("user");
        sparkSession.sql("select id,username,name,email from user").show();


        sparkSession.stop();


    }
}
